package karileet.meeting.profile;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

/**
 * Class notifies user about enabling (notification in notification bar) or 
 * disabling app (toast)
 * 
 * @author markus karileet
 */
public class ProfileNotifications {
	private static final int NOTIFICATION_ID = 1;
	private Context ctx;
	
	public ProfileNotifications(Context ctx) {
		this.ctx = ctx;
	}
	
	/**
	 * Method removes created notification
	 */
	public void cancelNotification() {
		Log.d(MainActivity.APP, "Starting 'cancelNotification' method");
		String ns = Context.NOTIFICATION_SERVICE;
		NotificationManager mNotificationManager = (NotificationManager) ctx.getSystemService(ns);
		mNotificationManager.cancel(NOTIFICATION_ID);

		// do a toast notification for user
		CharSequence text = ctx.getResources().getString(
				R.string.profile_deactivated_toast);
		int duration = Toast.LENGTH_LONG;
		Toast toast = Toast.makeText(ctx, text, duration);
		toast.show();
	}
	
	/**
	 * Method creates notification that profile is active
	 */
	public void initiateNotification() {
		Log.d(MainActivity.APP, "Starting 'initiateNotification' method");
		String ns = Context.NOTIFICATION_SERVICE;
		NotificationManager mNotificationManager = (NotificationManager) ctx.getSystemService(ns);
		int icon = R.drawable.ic_launcher;
		CharSequence tickerText = ctx.getResources().getString(
				R.string.profile_active_ticker);
		long when = System.currentTimeMillis();

		Notification notification = new Notification();
		notification.icon = icon;
		notification.tickerText = tickerText;
		notification.when = when;
		CharSequence contentTitle = ctx.getResources().getString(
				R.string.profile_active_ticker);
		CharSequence contentText = ctx.getResources().getString(
				R.string.profile_active_description);
		Intent notificationIntent = new Intent(ctx, MainActivity.class);
		PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0,
				notificationIntent, 0);

		notification.setLatestEventInfo(ctx, contentTitle, contentText,
				contentIntent);
		notification.flags |= Notification.FLAG_NO_CLEAR;
		mNotificationManager.notify(NOTIFICATION_ID, notification);
	}
}
